package com.caowei.weicimpress.weicimpress;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by caoweiPc on 2014/8/5.
 */
public class InputPasswdActivity extends InputBaseActivity implements View.OnClickListener{
    private static final String TAG=InputPasswdActivity.class.getSimpleName();
    @InjectView(R.id.list)
    ListView mListView;
    //都是需要密码的
    List<FileInfoBean> mBeans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBeans=getIntent().getParcelableArrayListExtra("beans");
        setContentView(R.layout.inputpasswdactivity);
        ButterKnife.inject(this);
        actiobarPath.setText(getString(R.string.inputPasswd));
        PasswdAdapter adapter=new PasswdAdapter();
        adapter.setmData((mBeans==null)?(new ArrayList<FileInfoBean>()):mBeans);
        mListView.setAdapter(adapter);
    }
    @OnClick({R.id.ok})
    @Override
    public void onClick(View v) {
        int id=v.getId();
        Intent intent= getIntent();
        String action=intent.getAction();
        if(action.equals(SysFileFragment.INPUTPASSWD_ACTION)){
            switch (id){
                case R.id.ok:
                    //validate password
                    Intent i=new Intent();
                    i.putParcelableArrayListExtra("beans",new ArrayList<FileInfoBean>(mBeans));
                    i.putExtra("currentDir",intent.getStringExtra("currentDir"));
                    i.putExtra("saveName",intent.getStringExtra("saveName"));
                    i.putExtra("preMsg",intent.getStringExtra("preMsg"));
                    setResult(RESULT_OK, i);
                    finish();
                    break;
            }
        }else if(action.equals(CompressFileFragment.INPUTPASSWD_ACTION)){
            switch (id){
                case R.id.ok:
                    Intent i=new Intent();
                    i.putParcelableArrayListExtra("beans",new ArrayList<FileInfoBean>(mBeans));
                    i.putParcelableArrayListExtra("childrenBeans",
                            getIntent().getParcelableArrayListExtra("childrenBeans"));
                    setResult(RESULT_OK, i);
                    finish();
                break;
            }
        }
    }
    class PasswdAdapter extends BaseAdapter{
        private List<FileInfoBean> mData;
        private int focusIndex=-1;
        @Override
        public int getCount() {
            return mData.size();
        }
        @Override
        public FileInfoBean getItem(int position) {
            return mData.get(position);
        }
        @Override
        public long getItemId(int position) {
            return ((Object)getItem(position)).hashCode();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=null;
            if(convertView==null){
                View v= LayoutInflater.from(InputPasswdActivity.this).inflate(R.layout.inputpasswdactivity_item,parent,false);
                viewHolder=new ViewHolder(v);
                v.setTag(viewHolder);
                convertView=v;
            }else{
                viewHolder= (ViewHolder) convertView.getTag();
            }
            viewHolder.name.setText(getItem(position).getFileName());
            TextWatcher preTextWatcher= (TextWatcher) viewHolder.editText.getTag();
            if(preTextWatcher!=null){
                viewHolder.editText.removeTextChangedListener(preTextWatcher);
            }
            TextWatcher textWatcher=new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    char[] passwdArr=s.toString().toCharArray();
                    getItem(position).setPasswd(passwdArr);
                }
                @Override
                public void afterTextChanged(Editable s) {

                }
            };
            viewHolder.editText.setHint(
                    InputPasswdActivity.this.getResources().getString(R.string.passwdhint));
            viewHolder.editText.setHintTextColor(getResources().getColor(R.color.gray_deep));
            viewHolder.editText.setTag(textWatcher);
            viewHolder.editText.addTextChangedListener(textWatcher);
            viewHolder.editText.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    focusIndex = position;
                    return false;
                }
            });

            char[] passwds= getItem(position).getPasswd();
            String passwd=null;
            if(passwds!=null){
                passwd=new String(passwds);
            }
            viewHolder.editText.setText(passwd);
            viewHolder.editText.clearFocus();
            if(focusIndex!=-1&&focusIndex==position){
                viewHolder.editText.requestFocus();
            }else if(position==0){
                viewHolder.editText.requestFocus();
            }
            return convertView;
        }
        public List<FileInfoBean> getmData() {
            return mData;
        }

        public void setmData(List<FileInfoBean> mData) {
            this.mData = mData;
        }
        class ViewHolder implements View.OnClickListener{
            @InjectView(R.id.edit)
            EditText editText;
            @InjectView(R.id.text)
            TextView name;
            @InjectView(R.id.close)
            ImageView close;
            public ViewHolder(View v){
                ButterKnife.inject(this,v);
            }
            @OnClick({R.id.close})
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        }
    }
}
